home *** CD-ROM | disk | FTP | other *** search
/ The Mac 1996 October / The Mac (October 1996).dmg / Web Authoring / parser10b3 Folder / Common Source / Parser.c next >
Encoding:
C/C++ Source or Header  |  1996-05-08  |  2.7 KB  |  122 lines  |  [TEXT/CWIE]

  1. /*
  2.     HTML Form Parser 1.0b3
  3.     ----------------------
  4.  
  5.     Written by Niel M. Bornstein
  6.     Copyright © 1996 PLR Software, Inc.
  7.  
  8.     This program is free software; you can redistribute it and/or modify it
  9.     under the terms of the GNU General Public License as published by the
  10.     Free Software Foundation; either version 2 of the License, or (at your
  11.     option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful, but
  14.     WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.     General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License along
  19.     with this program; if not, write to the Free Software Foundation, Inc.,
  20.     675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. #include "Parser.h"
  24.  
  25. void 
  26. parser(
  27.     Handle iBuffer, 
  28.     Handle oBuffer )
  29. {
  30.     char c;
  31.     long i, j = 0;
  32.     long offset = 0;
  33.     long s;
  34.     
  35.     s = GetHandleSize( iBuffer );
  36.     
  37.     for( i = 0; i < s; i++, j++ ) {
  38.         switch( (*iBuffer)[i] ) {
  39.             case '+':
  40.                 (*oBuffer)[j] = ' ';
  41.                 break;
  42.             case '%':
  43.                 c = HexToChar( &(*iBuffer)[i] );
  44.                 if( c == '\n' ) c = '\r';
  45.                 (*oBuffer)[j] = c;
  46.                 i += 2;
  47.                 break;
  48.             case '&':
  49.                 (*oBuffer)[j] = '\r';
  50.                 break;
  51.             case '\r':
  52.             case '\n':
  53.             case '\t':
  54.                 i++;
  55.                 break;
  56.             default:
  57.                 (*oBuffer)[j] = (*iBuffer)[i];
  58.                 break;
  59.         }
  60.     }
  61.     
  62.     SetHandleSize( oBuffer, j );
  63.     
  64.     return;
  65. }
  66.  
  67. char
  68. HexToChar(
  69.     char *h )
  70. {
  71.     static hextable mytab[] =  {  { '0',  0 }, { '1',  1 }, { '2',  2 }, { '3',  3 },  
  72.         { '4',  4 }, { '5',  5 }, { '6',  6 }, { '7',  7 }, { '8',  8 }, { '9',  9 },  
  73.         { 'A', 10 }, { 'B', 11 }, { 'C', 12 }, { 'D', 13 }, { 'E', 14 }, { 'F', 15 }, 
  74.         { 'a', 10 }, { 'b', 11 }, { 'c', 12 }, { 'd', 13 }, { 'e', 14 }, { 'f', 15 }, 
  75.         { '\0', 0 } };
  76.  
  77.     char c1 = 0, c2 = 0, c;
  78.     hextable *t = mytab;
  79.     
  80.     while ( t->c != '\0' ) {
  81.         if ( t->c == *(h+1) ) c1 = t->x;
  82.         if ( t->c == *(h+2) ) c2 = t->x;
  83.         t++;
  84.     }
  85.     c = c1 * 16 + c2;
  86.     
  87.     return c;
  88. }
  89.  
  90. /*
  91.     Alternative way to do it suggested by johns@efn.org (John Selhorst). I
  92.     opted for the table because I like tables.  I hate to assume anything
  93.     about the order of ascii characters (probably silly of me).  Also, I
  94.     didn't want to use ANSI functions (like sprintf and getc) because this
  95.     is going in a teeny little code resource.
  96.     
  97. char 
  98. HexToChar(
  99.     char *h )
  100. {
  101.     unsigned char cin = h[1], cout;
  102.     
  103.     if (cin >= '0' && cin <= '9')
  104.         cout = cin - '0';
  105.     else if (cin >= 'A' && cin <= 'F')
  106.         cout = cin - 'A' + 10;
  107.     else if (cin >= 'a' && cin <= 'f')
  108.         cout = cin - 'A' + 10;
  109.     else
  110.         cout = 0;
  111.         
  112.     cin = h[2];
  113.     if (cin >= '0' && cin <= '9')
  114.         cout += cin - '0';
  115.     else if (cin >= 'A' && cin <= 'F')
  116.         cout += cin - 'A' + 10;
  117.     else if (cin >= 'a' && cin <= 'f')
  118.         cout += cin - 'A' + 10;
  119.     return cout;
  120. }
  121. */
  122.